package zhibi.controller;

import com.github.pagehelper.PageInfo;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.commons.exception.MessageException;
import zhibi.domain.User;
import zhibi.extra.base.BaseController;
import zhibi.extra.context.Constant;
import zhibi.extra.pojo.OrdersPo;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;
import zhibi.service.OrdersService;
import zhibi.service.UserService;
import zhibi.utils.DateUtils;
import zhibi.utils.security.SecurityUtils;

import java.util.Date;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;

    @GetMapping("login")
    public String login() {
        return "login";
    }


    @RequestMapping("index")
    public String index(Page page, Model model) {
        String date = DateUtils.formatDate(DateUtils.rollDay(new Date(), 1), "yyyy-MM-dd");
        Example example = Example.getInstance()
                .addParam("o.godate", date, ExampleType.Operation.LTE)
                .addParam("o.godate", date, ExampleType.Operation.GTE)
                .addOrder("o.godate", ExampleType.OrderType.DESC);
        if(!sessionUser().getType().equals("admin")){
            example.addParam("o.userid",sessionUser().getId());
        }
        PageInfo<OrdersPo> pageInfo = ordersService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "orders/list";
    }

    @PostMapping("login")
    public String login(String username, String password) {
        User user = userService.login(username, password);
        if (null == user) return forward("login", "用户名或密码错误");
        logger.info("【 登 录 】 用户:{}  ", user.getName());
        session.setAttribute(Constant.SESSION.ADMIN, user);
        return redirect("index");
    }


    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(Constant.SESSION.ADMIN);
        return redirect("index");
    }

    @RequestMapping("modify")
    public String modify() {
        return "modify";
    }

    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public String modify(String pass, String newpass, String confimpass) {
        if (!newpass.equals(confimpass)) {
            throw new MessageException("两次密码不一样");
        }
        if (!sessionUser().getPassword().equalsIgnoreCase(SecurityUtils.MD5(pass))) {
            throw new MessageException("原密码错误");
        }
        User user = userService.selectByPrimaryKey(sessionUser().getId());
        user.setPassword(SecurityUtils.MD5(newpass));
        userService.updateByPrimaryKey(user);
        return redirect("logout");
    }

}
